#include "memslot.h"
#include "matrix.h"
#include "mxsetup.h"

/* BUF_STARTADDR from Kconfig(.heap) or `defslot_buf`(.bss) */
#ifdef MXSETUP_DEFSLOT_BUF_FROM_OTHER
    #if (MXSETUP_DEFSLOT_BUF_START == 0x00000000)
        #error please specify the start address of defslot buffer.
    #endif
    #define BUF_STARTADDR (mx_void_pt)MXSETUP_DEFSLOT_BUF_START
#else
    static mx_uint8_t defslot_buf[MXSETUP_DEFSLOT_BUF_SIZE];
    #define BUF_STARTADDR defslot_buf
#endif

/* initialize default memory slot */
static void defslot_init ()
{
#ifdef MXSETUP_DEFSLOT_TYPE_BLOCK
    /* mount a block slot to system */
    static mx_memslot_block_t defslot;
    mx_memslot_block_param_t param = {
        .id = 0,                                // fixed id
        .align = MX_CPU_BITWIDE_BYTE / 4 + 1,   // CPU ALIGNED
        .type = MX_MEMSLOT_TYPE_BLOCK,
        .perf = MXSETUP_DEFSLOT_PERF,
        .start = BUF_STARTADDR,
        .size = MXSETUP_DEFSLOT_BUF_SIZE,
        .bsize = MXSETUP_DEFSLOT_BSIZE
    };
#else
    /* mount a flat slot to system */
    static mx_memslot_flat_t defslot;
    mx_memslot_param_t param = {
        .id = 0,
        .align = MX_CPU_BITWIDE_BYTE / 4 + 1,
        .type = MX_MEMSLOT_TYPE_FLAT,
        .perf = MXSETUP_DEFSLOT_PERF,
        .start = BUF_STARTADDR,
        .size = MXSETUP_DEFSLOT_BUF_SIZE
    };
#endif
    /* mount to system */
    mx_memslot_mount((mx_memslot_pt)&defslot, (mx_memslot_param_pt)&param);
}
MX_BOARDINIT_EXPORT(defslot_init, 5);
